﻿Ext.define("PSI.OutConsignation.ConsignationDetailEditForm", {
    createUI: function (entity) {
        var me = this;

        var wnd = Ext.create("Ext.window.Window", {
            title: entity == null ? "新增送货明细" : "编辑送货明细",
            modal: true,
            onEsc: Ext.emptyFn,
            width: 500,
            height: 435,
            layout: "fit",
            items: [
                {
                    id: "editForm",
                    xtype: "form",
                    layout: "form",
                    height: "100%",
                    bodyPadding: 5,
                    defaultType: 'textfield',
                    fieldDefaults: {
                        labelWidth: 60,
                        labelAlign: "right",
                        labelSeparator: "",
                        msgTarget: 'side'
                    },
                    items: [
                        {
                            xtype: "hidden",
                            name: "Id",
                            value: entity == null ? null : entity.id
                        },
                        {
                            xtype: "hidden",
                            name: "ParentId",
                            value: me.parentId
                        },
                        {
                            id: "editBlNumber",
                            fieldLabel: "运单号",
                            name: "blNumber",
                            value: entity == null ? null : entity.billNumber
                        }, {
                            xtype: "psi_bc_vslfield",
                            id: "editVsl",
                            fieldLabel: "船名",
                            name: "vsl"
                        }, {
                            id: "editVoyNumber",
                            fieldLabel: "航次",
                            name: "voyNumber"
                        },
                        {
                            id: "editCntOwner",
                            fieldLabel: "箱属",
                            name: "cntOwner",
                            xtype: "psi_bc_cntownerfield"
                        }, {
                            id: "editAddress",
                            name: "address",
                            xtype: "psi_bc_addressfield",
                            fieldLabel: "门点",
                            allowBlank: false,
                            beforeLabelTextTpl: PSI.Const.REQUIRED,
                            blankText: "没有输入门点"
                        }, {
                            id: "editGoodsOwner",
                            name: "goodsOwner",
                            xtype: "psi_bc_customerfield",
                            fieldLabel: "货主"
                        }, {
                            id: "editGoodsOwnerTel",
                            name: "goodsOwnerTel",
                            xtype: "textfield",
                            fieldLabel: "货主电话"
                        }, {
                            id: "editGoods",
                            name: "goods",
                            xtype: "textfield",
                            fieldLabel: "货名"
                        }, {
                            id: "editTon",
                            name: "ton",
                            xtype: "textfield",
                            fieldLabel: "吨位"
                        }, {
                            id: "editLoadCntMan",
                            name: "loadCntMan",
                            xtype: "textfield",
                            fieldLabel: "装箱联系人"
                        }, {
                            id: "editLoadCntManTel",
                            name: "loadCntManTel",
                            xtype: "textfield",
                            fieldLabel: "装箱人电话"
                        }, {
                            id: "editDestPort",
                            fieldLabel: "目的港",
                            name: "destPort",
                            xtype: "psi_bc_portfield"
                        }, {
                            id: "editMemo",
                            name: "memo",
                            xtype: "textfield",
                            fieldLabel: "备注"
                        }, {
                            id: "editTruckCount",
                            name: "truckCount",
                            xtype: "numberfield",
                            fieldLabel: "计划拖车数",
                            hideTrigger: true,
                            keyNavEnabled: false,
                            mouseWheelEnabled: false,
                            value: 1,
                            minValue: 1
                        }
                    ],
                    buttons: [
                        {
                            text: "确定",
                            formBind: true,
                            iconCls: "psi-button-ok",
                            handler: me.onOK,
                            scope: me
                        },
                        {
                            text: "取消", handler: function () {
                                PSI.MsgBox.confirm("请确认是否取消操作?", function () {
                                    me.wnd.close();
                                });
                            }, scope: me
                        }
                    ]
                }
            ]

        });

        me.wnd = wnd;

        Ext.getCmp("editBlNumber").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editVsl").focus();
            }
        }, me);
        Ext.getCmp("editVsl").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editVoyNumber").focus();
            }
        }, me);
        Ext.getCmp("editVoyNumber").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editCntOwner").focus();
            }
        }, me);
        Ext.getCmp("editCntOwner").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editAddress").focus();
            }
        }, me);
        Ext.getCmp("editAddress").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editGoodsOwner").focus();
            }
        }, me);
        Ext.getCmp("editGoodsOwner").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editGoodsOwnerTel").focus();
            }
        }, me);
        Ext.getCmp("editGoodsOwnerTel").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editGoods").focus();
            }
        }, me);
        Ext.getCmp("editGoods").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editTon").focus();
            }
        }, me);
        Ext.getCmp("editTon").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editLoadCntMan").focus();
            }
        }, me);
        Ext.getCmp("editLoadCntMan").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editLoadCntManTel").focus();
            }
        }, me);
        Ext.getCmp("editLoadCntManTel").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editDestPort").focus();
            }
        }, me);
        Ext.getCmp("editDestPort").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editMemo").focus();
            }
        }, me);
        Ext.getCmp("editMemo").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                if (!entity) {
                    Ext.getCmp("editTruckCount").focus();
                }
            }
        }, me);

        Ext.getCmp("editBlNumber").on("blur", function (field) {
            var v = field.getValue();
            if (v) {
                field.setValue(v.toUpperCase());
            }
        }, me);
    },

    show: function (parentForm, parentId, entity) {
        var me = this;

        me.parentId = parentId;
        me.parentForm = parentForm;
        me.entity = entity;

        me.createUI(entity);

        me.wnd.on("show", function () { me.onEditFormShow(entity); }, me);

        me.wnd.show();
    },

    onEditFormShow: function (entity) {
        Ext.getCmp("editBlNumber").focus();
        if (!entity) {
            return;
        }

        //隐藏拖车数
        Ext.getCmp("editTruckCount").hide();

        var me = this;
        var form = me.wnd;
        var el = me.wnd.getEl() || Ext.getBody();

        el.mask("数据加载中...");
        Ext.Ajax.request({
            url: "/OutConsignation/GetOutConsignationDetail",
            method: "POST",
            params: { id: entity.id },
            callback: function (options, success, response) {
                el.unmask();
                if (success) {
                     var data = Ext.JSON.decode(response.responseText);
                     me.setEntityData(data);
                     var cc = entity.vsl.split("/");
                     Ext.getCmp("editVsl").setValue(cc[0]);
                     Ext.getCmp("editVoyNumber").setValue(cc[1]);
                   
                } else {
                    PSI.MsgBox.showInfo("网络错误");
                }
            }
        });
    },

    setEntityData: function (data) {
       
        Ext.getCmp("editBlNumber").setValue(data.blNumber);
        Ext.getCmp("editAddress").setValue(data.address);
        Ext.getCmp("editGoodsOwner").setValue(data.goodsOwner);
        Ext.getCmp("editGoodsOwnerTel").setValue(data.goodsOwnerTel);
        Ext.getCmp("editLoadCntMan").setValue(data.loadCntMan);
        Ext.getCmp("editLoadCntManTel").setValue(data.loadCntManTel);
        Ext.getCmp("editMemo").setValue(data.memo);
        Ext.getCmp("editCntOwner").setValue(data.cntOwner);
        Ext.getCmp("editGoods").setValue(data.goods);
        Ext.getCmp("editTon").setValue(data.ton);
        Ext.getCmp("editDestPort").setValue(data.destPort);
    },

    // private
    onOK: function () {
        var me = this;
        var f = Ext.getCmp("editForm");
        var el = f.getEl();

        el.mask("数据保存中...");
        f.submit({
            url: "/OutConsignation/EditOutConsignationDetail",
            method: "POST",
            success: function (form, action) {
                el.unmask();
                me.parentForm.refreshConsignationDetailGrid();

                var info = me.entity ? "数据保存成功" : "数据保存成功, 单击确定按钮后可以继续新增送货明细";
                PSI.MsgBox.showInfo(info, function () {
                    me.resetEditor();
                });
            },
            failure: function (form, action) {
                el.unmask();
                PSI.MsgBox.showInfo(action.result.msg, function () {
                    Ext.getCmp("editBlNumber").focus();
                });
            }
        });
    },

    resetEditor: function () {
        var me = this;

        Ext.getCmp("editBlNumber").setValue(null);
        Ext.getCmp("editCntOwner").setValue(null);

        Ext.getCmp("editVsl").setValue(null);
        Ext.getCmp("editVoyNumber").setValue(null);

        Ext.getCmp("editAddress").setValue(null);
        Ext.getCmp("editAddress").clearInvalid();

        Ext.getCmp("editGoodsOwner").setValue(null);
        Ext.getCmp("editGoodsOwner").clearInvalid();

        Ext.getCmp("editGoodsOwnerTel").setValue(null);
        Ext.getCmp("editGoods").setValue(null);
        Ext.getCmp("editLoadCntMan").setValue(null);
        Ext.getCmp("editLoadCntManTel").setValue(null);

        Ext.getCmp("editMemo").setValue(null);

        Ext.getCmp("editGoods").setValue(null);
        Ext.getCmp("editTon").setValue(null);
        Ext.getCmp("editDestPort").setValue(null);


        Ext.getCmp("editTruckCount").setValue(1);

        Ext.getCmp("editBlNumber").focus();

        if (me.entity) {
            me.wnd.close();
        }
    }
});